package view

import groovy.swing.SwingBuilder

import java.awt.BorderLayout
import java.awt.Color
import java.awt.Font

import javax.swing.BorderFactory
import javax.swing.JTable
import javax.swing.border.Border

import view.plotter.Plotter;
import model.Model
import au.com.bytecode.opencsv.CSVReader

class ControlPanel {

	Model model
	def path = ""
	def data = []
	def columnNames

	def txtRun
	
	//Plotter plotter
	/***
	 * 把有header的csv转化为tableModel可以用的list
	 * data是一个list，每个元素是一个map[header:value]
	 * 例如：
	 * tableModel(list: csv2tableData(path) )
	 * @param path csv文件路径
	 * @return tableModel可用的list
	 */
	def csv2tableData(path){
		CSVReader reader = new CSVReader(new FileReader(path));
		List datalist = reader.readAll();
		columnNames = datalist[0]
		datalist.remove(0)
		//JTable table = new JTable(myEntries.toArray());
		//		println columnNames
		//File sfile = new File(path)
		//println data
		datalist.each{ String[] line ->
			//if (line[0][0] != "#"){
			def linemap = [:]
			line.eachWithIndex { value, i ->
				linemap[columnNames[i]]=value
			}
			data.add(linemap)
			//}
		}
		//println data
		return data
	}
	def loadSettings(path){
		this.path = path

		data = csv2tableData(path)
	}

	def applySettings(){

		data.each{ prop ->
			String name = prop["Name"]
			def value = prop["Value"]
			if(name[0]!="/"){
				if (value.isNumber()){
					model."$name" = value.toFloat()
				}else{
					model."$name" = value
				}

			}
		}
	}

	def windowClosedHandler = {
		if(model != null){
			model.stop()
		}
	}
	
	def run(){
		model = new Model()
		//model.plotter = plotter
		applySettings()
		model.runs = 0
		model.setup()
		model.run()
	}

	def show(){
		JTable settingsTable

		def swing = new SwingBuilder();

		def frame = swing.frame(title: "控制面板" ,
		size:[400, 600],
		//location:[x, y],
		windowClosed:windowClosedHandler,
		defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE){
			def tablePanel = {
				scrollPane(){
					//settingsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
					def tmodel = tableModel(list:data){
						//TableColumnModel columns = new DefaultTableColumnModel()
						columnNames.eachWithIndex { value,i ->

							propertyColumn(header:value,propertyName:value)
						}
						//propertyColumn( header:'NickName', propertyName:'nick', modelIndex: 0, minWidth:50 )
						//addColumn( propertyColumn( header:'Full Name', propertyName:'full', modelIndex: 1, preferredWidth:200 ) )
						//propertyColumn(header:'Full Name', propertyName:'full', preferredWidth:200 )
					}
					settingsTable = table(model:tmodel,gridColor:java.awt.Color.LIGHT_GRAY,
					cellSelectionEnabled:true,
					autoCreateColumnsFromModel:true,
					//autoResizeMode:1,
					font:new Font("System",0,12))
					settingsTable.setRowHeight(20)
				}
			}

			def runtoHandler = { runs ->
				run()
			}
			
			def runHandler = {  runtoHandler(0) }

			def stopHandler = { model.stop() }

			def southPanel = {
				panel(constraints:BorderLayout.SOUTH){
					button("run",actionPerformed:runHandler)
					//button("run to",actionPerformed:runtoHandler)
					Border border = BorderFactory.createLineBorder(Color.BLACK);
					//def txtRun = textArea("2400",border:border,columns:6)
					//label("ticks")
					button("stop",actionPerformed:stopHandler)
					//text.setBorder(border)
				}
			}

			def mainPanel = panel(layout: new BorderLayout()){

				tablePanel()
				southPanel()

			}

		}
		frame.setVisible(true)
	}
}

